﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using iMapa.Backend;
using System.Windows;
using iMapa.Backend.Calibration;

namespace iMapa.UI.ModelView.Main
{
    public class GroupViewModel : INotifyPropertyChanged
    {
        private SimulationGroupsViewModel parent;
        private string name;
        private bool isNew = false;
        private InteractMapGroup igroup;
        private ObservableCollection<MapViewModel> maps = new ObservableCollection<MapViewModel>();
        private MainViewModel root;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                onPropertyChanged("Name");
            }
        }
        public bool IsNew
        {
            get { return isNew; }
            set
            {
                if (isNew != value)
                {
                    isNew = value;
                    if (value == false)
                    {
                        foreach (MapViewModel map in maps)
                        {
                            map.Imap.ParentGroup = name;
                            InteractMapLoader.Save(map.Imap);
                        }
                    }
                    onPropertyChanged("TextBoxVisibility");
                    onPropertyChanged("TextBlockVisibility");
                }
            }
        }

        public SimulationGroupsViewModel Parent
        {
            get { return parent; }
            set { parent = value; }
        }


        public ObservableCollection<MapViewModel> Maps
        {
            get { return maps; }
            set
            {
                maps = value;
            }
        }

        public Visibility TextBoxVisibility
        {
            get { return isNew ? Visibility.Visible : Visibility.Hidden; }
        }

        public Visibility TextBlockVisibility
        {
            get { return isNew ? Visibility.Hidden : Visibility.Visible; }
        }

        public GroupViewModel(MainViewModel root)
        {
            this.root = root;
        }

        public MainViewModel Root
        {
            get { return root; }
            set { root = value; }
        }


        public InteractMapGroup Igroup
        {
            get { return igroup; }
            set { igroup = value; }
        }

        public void onSimulationChanged()
        {
            onPropertyChanged("SimulationStopped");
            onPropertyChanged("SimulationInProgress");
            foreach (MapViewModel map in maps)
            {
                map.onCalibrationChanged();
            }
        }

        public void ResetAllCalibrations()
        {
            foreach (InteractMap map in igroup.Maps)
            {
                map.resetHomography();
            }
        }

        public Boolean SimulationInProgress
        {
            get { return SimulationController.getInstance().SimulationRunning; }
        }
        public Boolean SimulationStopped
        {
            get { return SimulationController.getInstance().SimulationStopped; }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void onPropertyChanged(String property)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion


    }
}
